package com.jbp.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbp.dao.ProtectGiftUserDao;
import com.jbp.model.ProtectGiftUser;
import com.jbp.model.User;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ProtectGiftUserService extends ServiceImpl<ProtectGiftUserDao, ProtectGiftUser> implements IService<ProtectGiftUser> {

    @Autowired
    private UserService userService;

    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ProtectGiftUser> queryWrapper = new QueryWrapper<>();
        if (params.get("userUuid") != null){
            queryWrapper.inSql("user_id", "select id from user WHERE uuid LIKE '%"+params.get("userUuid")+"%'");
        }
        if (params.get("userName") != null){
            queryWrapper.inSql("user_id", "select id from user WHERE name LIKE '%"+params.get("userName")+"%'");
        }
        if (params.get("id") != null) {
            queryWrapper.like("id",params.get("id"));
        }
        if (params.get("userId") != null) {
            queryWrapper.like("user_id",params.get("userId"));
        }
        if (params.get("createTime") != null) {
            queryWrapper.like("create_time",params.get("createTime"));
        }
        if (params.get("fromDate")!=null) {
            queryWrapper.ge("create_time", params.get("fromDate"));
        }
        if (params.get("toDate")!=null) {
            queryWrapper.le("DATE_FORMAT(create_time,   '%Y-%m-%d')", params.get("toDate"));
        }
        queryWrapper.orderByDesc("id");
        IPage<ProtectGiftUser> page = this.page(
            new Query<ProtectGiftUser>().getPage(params), queryWrapper
        );
        page.getRecords().forEach(record -> {
            User user = userService.getById(record.getUserId());
            record.setUserUuid(user.getUuid());
            record.setUserName(user.getName());
        });
        return new PageUtils(page);
    }
}